﻿using System;
using Presentation;
using Infrastructure;

namespace WebClient
{
    public partial class View : System.Web.UI.Page, IView
    {
        private Presenter presenter;
        private HtmlLogger htmlLogger;
        private TxtLogger txtLogger;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtLogger = new TxtLogger();
                htmlLogger = new HtmlLogger();
                Session["logger"] = txtLogger;
                Session["txtLogger"] = txtLogger;
                Session["htmlLogger"] = htmlLogger;
            }

            if (Session["presenter"] == null)
            {
                presenter = new Presenter(this);
                Session["presenter"] = presenter;
            }
            else
            {
                presenter = (Presenter)Session["presenter"];
                presenter.SetView(this);
            }
        }

        public string array
        {
            get
            {
                return (string)Session["array"];
            }
            set
            {
                Session["array"] = value;
            }
        }
        public string elementToFind
        {
            get
            {
                return (string)Session["elementToFind"];
            }
            set
            {
                Session["elementToFind"] = value;
            }
        }
        public string sortedArray
        {
            get
            {
                return (string)Session["sortedArray"];
            }
            set
            {
                Session["sortedArray"] = value;
            }
        }
        public string position
        {
            get
            {
                return (string)Session["position"];
            }
            set
            {
                Session["position"] = value;
            }
        }
        public string errorMessage
        {
            get
            {
                return (string)Session["errorMessage"];
            }
            set
            {
                Session["errorMessage"] = value;
            }
        }

        protected void ButtonSearch_Click(object sender, EventArgs e)
        {
            array = TextBoxArray.Text;
            elementToFind = TextBoxElementToFind.Text;

            presenter.Search();

            TextBoxSortedArray.Text = sortedArray;
            TextBoxPosition.Text = position;
            ErrorLabel.Text = errorMessage;

            presenter.WriteToLog((TxtLogger)Session["txtLogger"]);
            presenter.WriteToLog((HtmlLogger)Session["htmlLogger"]);
        }

        protected void ButtonShowLog_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(((ILogger)Session["logger"]).fileName);
        }

        protected void RadioGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (RadioGroup.SelectedIndex)
            {
                case 0:
                    Session["logger"] = Session["txtLogger"];
                    break;
                case 1:
                    Session["logger"] = Session["htmlLogger"];
                    break;
            }
        }
    }
}